<?php

	/**
	 * 
	 * Redistribution and use in source and binary forms, with or without
	 * modification, are permitted provided that the following conditions are met:
	 *
	 *     * Redistributions of source code must retain the above copyright
	 *       notice, this list of conditions and the following disclaimer.
	 *     * Redistributions in binary form must reproduce the above copyright
	 *       notice, this list of conditions and the following disclaimer in the
	 *       documentation and/or other materials provided with the distribution.
	 *     * Neither the name of the University of California, Berkeley nor the
	 *       names of its contributors may be used to endorse or promote products
	 *       derived from this software without specific prior written permission.
	 *
	 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY
	 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
	 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
	 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
	 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
	 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
	 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	 * 
	 * 
	 * Classe principal do pSQLite
	 * @author Bruno Pitteli Gonçalves <scorninpc<at>gmail<dot>com>
	 * @author Carlos Henrrique Marques <rickadt<at>gmail<dot>com>
	 * @version Revision 2
	 * @license BSD license
	 *
	 *	Change log
	 *	Revision 2:
	 *		Class initialize methods
	 */
	
	require_once("init.php");

	class pSQLite extends pSQLite_Interface
	{
		public $currentTab;
		public $countTab;
		public $openedTabs;
		public $objDB;
		public $stopSignal = FALSE;
		public $tabEdit = array();

		/**
		 * Save recent openned files
		 * @since Revision 9
		 * @name $recentFiles
		 * @access public
		 * @type Array
		 */
		public $recentFiles = array();

		/**
		 * Save result of executes
		 * @since Revision 9
		 * @name __objResult
		 * @access private
		 * @type Resource
		 */
		public $__objResult;

		/**
		 * Class contructor
		 * @since Revision 2
		 * @name __construct
		 * @access public
		 */
		public function __construct()
		{
			parent::__construct();
			Gtk::main();
		}

		/**
		 * Unload of frmMain
		 * @since Revision 2
		 * @name frmMain_unload
		 * @access public
		 */
		public function frmMain_unload()
		{
			// Save recent openned
			file_put_contents("recents.dat", implode("\n", $this->recentFiles));
			Gtk::main_quit();
			return FALSE;
		}

		/**
		 * Load of frmMain
		 * @since Revision 2
		 * @name frmMain_onload
		 * @access public
		 */
		public function frmMain_onload()
		{
			// Open recents.dat
			if(file_exists("recents.dat"))
			{
				// Load and create line-line (recentFile)
				$this->recentFiles = file("recents.dat", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
				$this->__loadRecentOpenned();
			}
			else
			{
				// Create file
				touch("recents.dat");
			}
			
			$this->widgets['ntbQueries']->set_show_tabs(FALSE);
			$this->countTab = 1;
			$this->openedTabs = 1;
			$this->createQueryTab($this->openedTabs);
			$this->currentTab = 0;
		}

		/**
		 * On change of ntbQueries
		 * @since Revision 2
		 * @name ntbQueries_onchange
		 * @access public
		 * @param $widget - Resource
		 * @param $point - Array
		 * @param $selectedTab - integer
		 */
		public function ntbQueries_onchange($widget, $point, $selectedTab)
		{
			$this->currentTab = $selectedTab;
			if(@is_array($this->tabEdit[$this->currentTab]))
			{
				$this->widgets['tolBar']['cancel']->set_sensitive(TRUE);
				$this->widgets['tolBar']['apply']->set_sensitive(TRUE);
				$this->widgets['tolBar']['execute']->set_sensitive(FALSE);
				$this->widgets['mnuBar']['script']['cancel']->set_sensitive(TRUE);
				$this->widgets['mnuBar']['script']['apply']->set_sensitive(FALSE);
				$this->widgets['mnuBar']['script']['execute']->set_sensitive(FALSE);
			}
			else
			{
				$this->widgets['tolBar']['cancel']->set_sensitive(FALSE);
				$this->widgets['tolBar']['apply']->set_sensitive(FALSE);
				$this->widgets['mnuBar']['script']['cancel']->set_sensitive(FALSE);
				$this->widgets['mnuBar']['script']['apply']->set_sensitive(FALSE);
				if($this->objDB != NULL)
				{
					$this->widgets['tolBar']['execute']->set_sensitive(TRUE);
					$this->widgets['mnuBar']['script']['execute']->set_sensitive(TRUE);
				}
			}
		}

		/**
		 * On button press in trvStruct
		 * @since Revision 2
		 * @name trvStruct_buttonpress
		 * @access public
		 * @param $widget - Resource
		 * @param $event - Array
		 * @return Bool
		 */
		public function trvStruct_buttonpress($widget, $event)
		{
			if($event->type == 5)
			{
				// Get table name
				list($model, $iter) = $widget->get_selection()->get_selected();
				if (!$iter) 
				{
					return FALSE;
				}
				$tableName = $model->get_value($iter, 0);
				
				$scintilla = $this->__getScintillaFromTab($this->currentTab);
				$scintilla->set_text("SELECT * FROM " . $tableName . " LIMIT 0, 1000");
			}
		}
		
		/**
		 * On click in tolBar
		 * @since Revision 2
		 * @name tolBar_onclick
		 * @access public
		 * @param $index - String
		 * @return Bool
		 */
		public function tolBar_onclick($index)
		{
			switch($index)
			{
				case "open":
					$dialog = new GtkFileChooserDialog(
						'Selecione um arquivo',
						NULL,
						Gtk::FILE_CHOOSER_ACTION_OPEN,
						array(
							Gtk::STOCK_OK,
							Gtk::RESPONSE_OK,
							Gtk::STOCK_CANCEL,
							Gtk::RESPONSE_CANCEL
						)
					);
					if($dialog->run() == Gtk::RESPONSE_OK)
					{
						$path = $dialog->get_filename();
						$this->__openDataBase($path);
					}
					$dialog->destroy();
					
				break;
				
				case "new":
					$this->countTab++;
					$this->openedTabs++;
					$this->currentTab = $this->createQueryTab($this->openedTabs);
					$this->widgets['ntbQueries']->set_current_page($this->currentTab);
					if($this->countTab > 1)
					{
						$this->widgets['ntbQueries']->set_show_tabs(TRUE);
						$this->widgets['tolBar']['close']->set_sensitive(TRUE);
						$this->widgets['mnuBar']['files']['closetab']->set_sensitive(TRUE);
					}
				break;
				
				case "close":
					$selectedTab = $this->currentTab;
					$this->countTab--;
					$this->widgets['ntbQueries']->remove_page($this->widgets['ntbQueries']->get_current_page());
					if($this->countTab == 1)
					{
						$this->widgets['ntbQueries']->set_show_tabs(FALSE);
						$this->widgets['tolBar']['close']->set_sensitive(FALSE);
						$this->widgets['mnuBar']['files']['closetab']->set_sensitive(FALSE);
					}
					
					// Remove edit tab
					unset($this->tabEdit[$selectedTab]);
					
					// Recreate index
					for($i=$selectedTab+1; $i<=$this->countTab; $i++)
					{
						if(@is_array($this->tabEdit[$i]))
						{
							$this->tabEdit[$i-1] = $this->tabEdit[$i];
							unset($this->tabEdit[$i]);
						}
					}
				break;
				
				case "execute":
					$this->widgets['tolBar']['execute']->set_sensitive(FALSE);
					$this->widgets['tolBar']['stop']->set_sensitive(TRUE);
					$this->widgets['mnuBar']['script']['execute']->set_sensitive(FALSE);
					$this->widgets['mnuBar']['script']['stop']->set_sensitive(TRUE);
					
					$this->__changeColumns($this->currentTab, array(''));
					$sql = $this->__getSQLFromTab($this->currentTab);
					Gtk::main_iteration();
					$this->__objResult = $this->objDB->Execute($sql);
					Gtk::main_iteration();
					if($this->__objResult === FALSE)
					{
						$this->__setStatusMessage(utf8_decode($this->objDB->LastError));
					}
					elseif(!is_bool($this->__objResult))
					{
						$treeview = $this->__changeColumns($this->currentTab, $this->__objResult->FieldsName());
						$model = $treeview->get_model();
						$currentRow = 0;
						$totalRows = $this->__objResult->RecordCount();
						while($this->__objResult->EOF)
						{
							// Show porcent of execution
							$porcent = ++$currentRow * 100 / $totalRows;
							$porcent = (int)$porcent / 100;
							$this->widgets['pgsBar']->set_fraction($porcent);
							
							// Show data
							$fields = array();
							Gtk::main_iteration();
							for($i=0; $i<$this->__objResult->FieldsCount(); $i++)
							{
								$fields[] = $this->__objResult->Fields[$i];
							}
							Gtk::main_iteration();
							$model->append($fields);
							$this->__objResult->MoveNext();
							
							// Stop signal
							if($this->stopSignal)
							{
								$this->stopSignal = FALSE;
								return FALSE;
							}
						}
					}
					
					$this->__setStatusMessage($totalRows . " Rows");
					$this->widgets['pgsBar']->set_fraction(0);
					$this->widgets['tolBar']['execute']->set_sensitive(TRUE);
					$this->widgets['tolBar']['stop']->set_sensitive(FALSE);
					$this->widgets['mnuBar']['script']['execute']->set_sensitive(TRUE);
					$this->widgets['mnuBar']['script']['stop']->set_sensitive(FALSE);
				break;
				
				case "stop":
					$this->stopSignal = TRUE;
					$this->widgets['tolBar']['execute']->set_sensitive(TRUE);
					$this->widgets['tolBar']['stop']->set_sensitive(FALSE);
					$this->widgets['mnuBar']['script']['execute']->set_sensitive(TRUE);
					$this->widgets['mnuBar']['script']['stop']->set_sensitive(FALSE);
					
					$this->objDB->reconnect();
				break;
				
				case "edit":
					// Get selected table name
					$selected = $this->widgets['trvStruct']->get_selection();
					list($model, $iter) = $selected->get_selected();
					if($iter === NULL)
					{
						$this->__setStatusMessage("Nenhuma tabela selecionada");
						return FALSE;
					}
					$tableName = $model->get_value($iter, 0);
					
					// Get primary keys
					$result = $this->objDB->Execute("PRAGMA table_info(" . $tableName . ")");
					$i = 0;
					while($result->EOF) {
						$tableInfo['struct'][$i] = $result->Fields['name'];
						if($result->Fields['pk'] == 1)
						{
							$tableInfo['pk'][$i] = $result->Fields['name'];
						}
						$result->MoveNext();
						$i++;
					}
					
					// Create a new tab
					$this->tolBar_onclick("new");
					
					// Create the query
					$scintilla = $this->__getScintillaFromTab($this->currentTab);
					$scintilla->set_text("SELECT * FROM " . $tableName);
					
					// Add table name
					$tableInfo['tablename'] = $tableName;
					
					// Execute the query
					$this->tolBar_onclick("execute");
					
					// Add total paths
					$tableInfo['totallines'] = $this->__objResult->RecordCount();
					
					// Disable execute button
					$this->widgets['tolBar']['execute']->set_sensitive(FALSE);
					$this->widgets['mnuBar']['script']['execute']->set_sensitive(FALSE);
					
					// Set treeview editable
					$this->__setEditable($this->currentTab);
					
					// Enable bottons
					$this->widgets['tolBar']['apply']->set_sensitive(TRUE);
					$this->widgets['tolBar']['cancel']->set_sensitive(TRUE);
					$this->widgets['mnuBar']['script']['apply']->set_sensitive(TRUE);
					$this->widgets['mnuBar']['script']['cancel']->set_sensitive(TRUE);
					
					// Add info to array
					$tableInfo['SQL'] = "";
					$this->tabEdit[$this->currentTab] = $tableInfo;
					
					// Add blank line to add new values
					$this->__addBlankLine();
				break;

				case "apply":
					$tableInfo = &$this->tabEdit[$this->currentTab];
					
					// Get treeview
					$scrolled = $this->__getTreeviewFromTab($this->currentTab);
					$treeview = $scrolled->get_child()->get_child();
					$model = $treeview->get_model();
					
					// Create INSERTS sql
					$columns = $treeview->get_columns();
					foreach($tableInfo['newline'] as $path)
					{
						$iter = $model[$path]->iter;
						$SQLinsert	= "INSERT INTO " . $tableInfo['tablename'];
						$SQLvalues	= "";
						$SQLnames	= "";
						for($i=0; $i<count($columns); $i++)
						{
							$value = $model->get_value($iter, $i);
							if(strlen($value) > 0)
							{
								$SQLnames	.= $tableInfo['struct'][$i] . ", ";
								$SQLvalues	.= "'" . $value . "', ";
							}
						}
						$SQLnames	= substr($SQLnames, 0, strlen($SQLnames) - 2);
						$SQLvalues	= substr($SQLvalues, 0, strlen($SQLvalues) - 2);
						
						$sql = $SQLinsert . " (" . $SQLnames . ") VALUES (" . $SQLvalues . ")";
						$tableInfo['SQL'] .= $sql . ";\n";
					}
					
					// Update table
					$this->objDB->MultipleExecute($tableInfo['SQL']);
					
					
					// Clean buff
					$this->tabEdit[$this->currentTab]['SQL'] = "";
					$this->tabEdit[$this->currentTab]['newline'] = array();
					$this->tabEdit[$this->currentTab]['linecolor'] = array();
					
					// Recreate select
					$scintilla = $this->__getScintillaFromTab($this->currentTab);
					$scintilla->set_text("SELECT * FROM " . $tableInfo['tablename']);
					
					// Execute the query
					$this->tolBar_onclick("execute");
					
					// Add total paths
					$tableInfo['totallines'] = $this->__objResult->RecordCount();
					
					// Set treeview editable
					$this->__setEditable($this->currentTab);
					
					// Disable execute button
					$this->widgets['tolBar']['execute']->set_sensitive(FALSE);
					$this->widgets['mnuBar']['script']['execute']->set_sensitive(FALSE);
					
					// Add blank line to add new values
					$this->__addBlankLine();
				break;

				case "cancel":
					$tableInfo = &$this->tabEdit[$this->currentTab];
					
					// Clean buff
					$this->tabEdit[$this->currentTab]['SQL'] = "";
					$this->tabEdit[$this->currentTab]['newline'] = array();
					$this->tabEdit[$this->currentTab]['linecolor'] = array();
					
					// Recreate select
					$scintilla = $this->__getScintillaFromTab($this->currentTab);
					$scintilla->set_text("SELECT * FROM " . $this->tabEdit[$this->currentTab]['tablename']);
					
					// Execute the query
					$this->tolBar_onclick("execute");
					
					// Add total paths
					$tableInfo['totallines'] = $this->__objResult->RecordCount();
					
					// Set treeview editable
					$this->__setEditable($this->currentTab);
					
					// Disable execute button
					$this->widgets['tolBar']['execute']->set_sensitive(FALSE);
					$this->widgets['mnuBar']['script']['execute']->set_sensitive(FALSE);
					
					// Add blank line to add new values
					$this->__addBlankLine();
				break;
			}
			return TRUE;
		}
		
		
		/**
		 * On active in mnuRecents
		 * @since Revision 9
		 * @name mnuRecents_onactivate
		 * @access public
		 * @param $path - String
		 */
		public function mnuRecents_onactivate($path){
			$this->__openDataBase($path);
		}		

		/**
		 * On change result treeview
		 * @since Revision 2
		 * @name trvResult_onChange
		 * @access public
		 * @param $cellrenderertext - Resource
		 * @param $path - Integer
		 * @param $text - String
		 * @param $col - Integer
		 * @return Bool
		 */
		public function trvResult_onChange($cellrenderertext, $path, $text, $col)
		{
			// Get Scrolled/Treeview/Model
			$scrolled = $this->__getTreeviewFromTab($this->currentTab);
			$treeview = $scrolled->get_child()->get_child();
			$model = $treeview->get_model();
			$iter = $model[$path]->iter;
			$tableInfo = &$this->tabEdit[$this->currentTab];
			
			// Show new value
            $model->set($iter, $col, $text);
            
			if($path+1 > $tableInfo['totallines'])
			{
				if(strlen($text) <= 0)
				{
					return FALSE;
				}
				
				// Show new value
				$model->set($iter, $col, $text);
				
				// Verify of existing path
				foreach($tableInfo['newline'] as $newPath)
				{
					if($newPath == $path)
					{
						return FALSE;
					}
				}
				
				// Add the new path and new line
				$tableInfo['newline'][] = $path;

				// Set line color
				$tableInfo['linecolor'][$path] = "#55BB55";
				
				$this->__addBlankLine();
			}
			else
			{
				// Find Primary Key value
				$sqlWhere = "";
				reset($tableInfo['pk']);
				while(key($tableInfo['pk']) !== NULL)
				{
					$key = key($tableInfo['pk']);
					$value = $model->get_value($iter, $key);
					$sqlWhere .= $tableInfo['struct'][$key] . " = '" . $value . "' and";
					next($tableInfo['pk']);
				}
				$sqlWhere = substr($sqlWhere, 0, strlen($sqlWhere) - 4);
				
				// Create update SQL
				$sql = "UPDATE " . $tableInfo['tablename'];
				$sql .= " SET " . $tableInfo['struct'][$col];
				$sql .= " = '" . $text . "'";
				$sql .= " WHERE " . $sqlWhere;
				
				// Set end line of query and append to queries
				$tableInfo['SQL'] .= $sql . ";\n";

				// Set line color
				$tableInfo['linecolor'][$path] = "#CCCC55";
			}
			
 			// Change color of cell
			$treeview->queue_draw();
			
            return TRUE;
		}

		/**
		 * Get Treeview from tab index
		 * @since Revision 2
		 * @name __getTreeviewFromTab
		 * @access private
		 * @param $tabIndex - Integer
		 * @return Resource
		 */
		private function __getTreeviewFromTab($tabIndex)
		{
			$child = $this->widgets['ntbQueries']->get_nth_page($tabIndex);
			return $child->get_child2();
		}

		/**
		 * Get Scintilla from tab index
		 * @since Revision 2
		 * @name __getScintillaFromTab
		 * @access private
		 * @param $tabIndex - Integer
		 * @return Resource
		 */
		private function __getScintillaFromTab($tabIndex)
		{
			$child = $this->widgets['ntbQueries']->get_nth_page($tabIndex);
			return $child->get_child1();
		}

		/**
		 * Get SQL from scintilla from tab index
		 * @since Revision 2
		 * @name __getSQLFromTab
		 * @access private
		 * @param $tabIndex - Integer
		 * @return String
		 */
		private function __getSQLFromTab($tabIndex)
		{
			$scintilla = $this->__getScintillaFromTab($tabIndex);
			return $scintilla->get_text();
		}

		/**
		 * Recreate treeviewof results
		 * @since Revision 2
		 * @name __changeColumns
		 * @access private
		 * @param $tabIndex - Integer
		 * @param $columns - Array
		 * @return String
		 */
		private function __changeColumns($tabIndex, $columns=array())
		{
			// Get scrolled window
			$scrolled = $this->__getTreeviewFromTab($tabIndex);
			
			// Get treeview
			$treeview = $scrolled->get_child();
			
			// Remove treeview
			$scrolled->remove($treeview);
			
			// Create a new treeview
			$treeview = $this->createTreeView($columns);
			
			// Add new treeview in scrolled window
			$scrolled->add_with_viewport($treeview);
			
			// Show again
			$scrolled->show_all();

			return $treeview;
		}

		/**
		 * Send message to statusbar
		 * @since Revision 2
		 * @name __setStatusMessage
		 * @access private
		 * @param $message - String
		 */
		private function __setStatusMessage($message)
		{
			$this->widgets['stsBar']->set_text($message);
			// Remove message from status in 4s
			Gtk::timeout_add(4000, array($this, '__clearStatus'));
		}

		/**
		 * Remove message to statusbar
		 * @since Revision 2
		 * @name __clearStatus
		 * @access private
		 * @return Bool
		 */
		public function __clearStatus()
		{
			$this->widgets['stsBar']->set_text("");
			return FALSE;
		}

		/**
		 * Set treeview editable
		 * @since Revision 2
		 * @name __setEditable
		 * @param $tabIndex - Integer
		 * @access private
		 */
		private function __setEditable($tabIndex)
		{
			// Set editable
			$scrolled = $this->__getTreeviewFromTab($tabIndex);
			$viewport = $scrolled->get_child();
			$treeview = $viewport->get_child();
			$columns = $treeview->get_columns();
			$i = 0;
			foreach($columns as $column)
			{
				$renders = $column->get_cell_renderers();
				$renders[0]->set_property('editable', true);
				$renders[0]->connect('edited',  array($this, 'trvResult_onChange'), $i);
				$i++;
			}
		}

		/**
		 * Open database and adjust window, toolbar and menus
		 * @since Revision 9
		 * @name __openDataBase
		 * @param $path - String
		 * @access private
		 */
		private function __openDataBase($path)
		{
			$this->objDB = new SQLITE($path);
			if($this->objDB->LastError === FALSE)
			{
				$this->__setStatusMessage(utf8_decode("Não foi possivel abrir o banco de dados"));
				$this->widgets['frmMain']->set_title(utf8_decode("pSQLite :: Sem banco de dados."));
				$this->widgets['tolBar']['new']->set_sensitive(FALSE);
				$this->widgets['tolBar']['execute']->set_sensitive(FALSE);
				$this->widgets['tolBar']['edit']->set_sensitive(FALSE);
				$this->widgets['tolBar']['cancel']->set_sensitive(FALSE);
				$this->widgets['tolBar']['apply']->set_sensitive(FALSE);
				$this->widgets['mnuBar']['files']['newtab']->set_sensitive(FALSE);
				$this->widgets['mnuBar']['script']['execute']->set_sensitive(FALSE);
				$this->widgets['mnuBar']['files']['editquery']->set_sensitive(FALSE);
				$this->widgets['mnuBar']['script']['cancel']->set_sensitive(FALSE);
				$this->widgets['mnuBar']['script']['apply']->set_sensitive(FALSE);
			}
			else
			{
				$this->widgets['frmMain']->set_title(utf8_decode("pSQLite :: " . $path));
				$this->widgets['tolBar']['new']->set_sensitive(TRUE);
				$this->widgets['tolBar']['execute']->set_sensitive(TRUE);
				$this->widgets['tolBar']['edit']->set_sensitive(TRUE);
				$this->widgets['tolBar']['cancel']->set_sensitive(FALSE);
				$this->widgets['tolBar']['apply']->set_sensitive(FALSE);
				$this->widgets['mnuBar']['files']['newtab']->set_sensitive(TRUE);
				$this->widgets['mnuBar']['script']['execute']->set_sensitive(TRUE);
				$this->widgets['mnuBar']['files']['editquery']->set_sensitive(TRUE);
				$this->widgets['mnuBar']['script']['cancel']->set_sensitive(FALSE);
				$this->widgets['mnuBar']['script']['apply']->set_sensitive(FALSE);

				// Add file to recent openned array and remove duplacates
				$this->recentFiles = array_merge(array(0=>utf8_encode($path)), $this->recentFiles);
				$this->recentFiles = array_unique($this->recentFiles);

				// Load recend openned again
				$this->__loadRecentOpenned();
				
				// Load treeview with tables of data base
				$model = $this->widgets['trvStruct']->get_model();
				$model->clear();
				Gtk::main_iteration();
				$result = $this->objDB->Execute("SELECT name FROM sqlite_master WHERE type='table'");
				Gtk::main_iteration();
				if($result === FALSE)
				{
					$this->__setStatusMessage(utf8_decode($this->objDB->LastError));
				}
				else
				{
					Gtk::main_iteration();
					while($result->EOF)
					{
						$model->append(array($result->Fields[0]));
						$result->MoveNext();
					}
					Gtk::main_iteration();
				}
			}
		}

		/**
		 * Create menu of recent openned files
		 * @since Revision 9
		 * @name __loadRecentOpenned
		 * @access private
		 */
		private function __loadRecentOpenned()
		{
			$this->widgets['mnuRecents'] = new GtkMenu();
			$this->widgets['tolBar']['open']->set_menu($this->widgets['mnuRecents']);
			
			foreach($this->recentFiles as $recentFile)
			{
				$this->addRecentOpennedMenu(utf8_decode($recentFile));
			}

			$this->widgets['mnuBar']['files']['openrecent']->set_submenu($this->widgets['mnuRecents']);
		}

		/**
		 * Create new blank line to add new values
		 * @since Revision 9
		 * @name __addBlankLine
		 * @access private
		 */
		private function __addBlankLine()
		{
			// Add blank line to add new values
			$scrolled = $this->__getTreeviewFromTab($this->currentTab);
			$treeview = $scrolled->get_child()->get_child();
			$model = $treeview->get_model();

			$columns = $treeview->get_columns();
			$fields = array();
			for($i=0; $i<count($columns); $i++)
			{
				$fields[] = "";
			}
			$iter = $model->append($fields);

			$path = $model->get_path($iter);
    		$this->tabEdit[$this->currentTab]['linecolor'][$path[0]] = "#FFFDCC";
			
    		// Change color of cell
			$treeview->queue_draw();
		}
	} // class pSQLite extends pSQLite_Interface

	/**
	 * Initialize application
	 */
	new pSQLite();
